//
//  RCHomeCell.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/12.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit
import SnapKit

class RCHomeCell: UITableViewCell {
    
    var bottomConstraint:Constraint?
    //来一个模型属性
    var userStatus:RCUserStatus? {
        didSet{
            //给原创微博传递模型数据
            originalView.userStatus = userStatus
            //如果有转发微博 就给转发的View设置数据 传递模型            
            //但会有cell的复用问题 还得再没有转发微博的时候赋nil
            
            //为避免约束多余 要先卸载约束
            bottomConstraint?.uninstall()
            if let re = userStatus?.retweeted_status {
                retweetedView.hidden = false
                retweetedView.status = re
                //有转发微博时 cell底部参照转发微博底部
                bottomView.snp_updateConstraints { (make) -> Void in
                    bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).offset(10).constraint
                }

            }else {
                //解除原理 在一个分支内设置了数据,一定在else 分支内 设置其他数据才能够解除cell的复用
                retweetedView.hidden = true
                retweetedView.status = nil
                //没有转发微博是 cell底部参照原创微博底部
                bottomView.snp_updateConstraints { (make) -> Void in
                    bottomConstraint = make.top.equalTo(originalView.snp_bottom).offset(10).constraint
                }
                
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //不能写在super上面
        self.selectionStyle = .None
        
        //子视图要添加在contentView上 不能添加在self
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        setContraints()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setContraints() {
        //约束要参照contentView 不能写self
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
        }

        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)
            
        }
        
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
            self.bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).offset(10).constraint
//          make.bottom.equalTo(contentView.snp_bottom)
        }
        
        
        //开启自动布局之后 所有的frame布局就会自动失效
        //设置估算行高之后 要约束一下contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
  
        
    }
    
    //原创微博View
    lazy var originalView:RCOriginalView = RCOriginalView()
    //底部按钮View
    lazy var bottomView:RCCellBottomView = RCCellBottomView()
    //转发微博
    lazy var retweetedView:RCRetweetedView = RCRetweetedView()
    
    
}
